package com.stations.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.stations.common.web.domain.request.PageDomain;
import com.stations.common.web.domain.response.Result;
import com.stations.domain.SysRegion;
import com.stations.domain.TStation;
import com.stations.domain.TTransmitter;
import com.stations.domain.dtree.CheckArr;
import com.stations.domain.dtree.DTree;
import com.stations.mapper.SysRegionMapper;
import com.stations.mapper.TStationMapper;
import com.stations.service.StationService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


/**
 * Describe: 用户服务实现类
 * Author: Ljy
 * CreateTime: 2019/10/23
 */
@Service
public class StationServiceImpl extends ServiceImpl<TStationMapper, TStation> implements StationService {

    @Resource
    TStationMapper tStationMapper;

    @Resource
    SysRegionMapper sysRegionMapper;

    @Override
    public List<TStation> selectAll() {
        return tStationMapper.getAllStation();
    }

    @Override
    public Result add(TStation station) {
        int insert = tStationMapper.insert(station);
        return insert ==1 ? Result.success("添加台站成功!"):Result.failure("添加台站失败!");
    }

    @Override
    public Result edit(TStation station) {
        int i = tStationMapper.updateById(station);
        return i == 1 ? Result.success("修改台站成功!"):Result.failure("修改台站失败!");
    }

    @Override
    public Result del(TStation station) {
        int i = tStationMapper.deleteById(station);
        return i == 1 ? Result.success("删除台站成功!"):Result.failure("删除台站失败!");
    }

    //递归，建立子树形结构
    @Override
    public List<DTree> getTree(){
        List<DTree> listRegion = builTree();
        return listRegion;
    }

    @Override
    public PageInfo<TStation> selectTStationPage(TStation tStation, PageDomain pageDomain) {
        PageHelper.startPage(pageDomain.getPage(), pageDomain.getLimit());
        List<TStation> data = tStationMapper.selectTStationList(tStation);
        return new PageInfo<>(data);
    }

    //建立树形结构
    public List<DTree> builTree(){
        List<SysRegion> listRegion = sysRegionMapper.selectSysRegion();
        List<DTree> listDTree = new ArrayList<>();
        List<CheckArr> checkArrs=new ArrayList<>();
        DTree dTree;
        CheckArr checkArr =new CheckArr();
        checkArr.setType("0");
        checkArr.setIsChecked("0");
        checkArrs.add(checkArr);
        for (int i = 0; i < listRegion.size(); i++) {
            dTree = new DTree();
            dTree.setId(listRegion.get(i).getId());
            dTree.setParentId(listRegion.get(i).getPid());
            dTree.setTitle(listRegion.get(i).getName());
            dTree.setCheckArr(checkArrs);
            dTree.setIconClass("-1");
            listDTree.add(dTree);
        }

        List<DTree> treeDTrees =new  ArrayList<DTree>();
        for(DTree treeNode : getRootNode(listDTree)) {
            treeNode=buildChilTree(treeNode,listDTree);
            treeDTrees.add(treeNode);
        }
        return treeDTrees;
    }

    //递归，建立子树形结构
    public DTree buildChilTree(DTree pNode,List<DTree> listRegion){
        List<DTree> chilMenus =new  ArrayList<DTree>();
        for(DTree regionNode : listRegion) {
            if(regionNode.getParentId().equals(pNode.getId())) {
                chilMenus.add(buildChilTree(regionNode,listRegion));
            }
        }
        pNode.setChildren(chilMenus);
        return pNode;
    }

    //获取根节点(获取所有的父节点)
    public List<DTree> getRootNode(List<DTree> listDTree) {
        List<DTree> rootRegionLists =new  ArrayList<DTree>();
        for(DTree regionNode : listDTree) {
            if(regionNode.getParentId().equals("0")) {
                rootRegionLists.add(regionNode);
            }
        }
        return rootRegionLists;
    }
}
